/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufc.quixada.adrs.dao;

import br.ufc.quixada.adrs.dao.config.PostGresMapConfig;
import br.ufc.quixada.adrs.model.Contato;
import br.ufc.quixada.adrs.model.Supervisor;
import java.sql.SQLException;

/**
 *
 * @author Franklin
 */
public class SupervisorDAO {
    public void insert(Supervisor supervisor) throws SQLException {
        Long id = getProxId();
        supervisor.setId(id);
        PostGresMapConfig.getSqlMapClient().insert("addSupervisor", supervisor);
    }

    public void update(Supervisor supervisor) throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdContato");
        if (id == null) {
            id = 0L;
        }
        for (Contato d : supervisor.getUsuario().getContatos()) {
            d.setId(++id);
        }
        PostGresMapConfig.getSqlMapClient().update("updateSupervisor", supervisor);
    }

    public void delete(Long id) throws SQLException {
        PostGresMapConfig.getSqlMapClient().delete("deleteSupervisor", id);
    }

    public Supervisor getByUsuarioId(Long id) throws SQLException{
        return (Supervisor) PostGresMapConfig.getSqlMapClient().queryForObject("getSupervisorByUsuarioId", id);
    }

    public Supervisor getById(Long id) throws SQLException{
        return (Supervisor) PostGresMapConfig.getSqlMapClient().queryForObject("getSupervisorById", id);
    }

    private Long getProxId() throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxId");
        if (id == null) {
            id = 0L;
        }
        return id + 1L;
    }

}
